/**
 * Created by lwy on 2017/5/21.
 */
var shopId = GetQueryString('shopId');
var rate;
    $(function(){
        getRate();
    })

//获取提点比率
function getRate(){
    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }
    $.ajax({
        url:'/shopC/geShopRate.json',
        dataType:'json',
        type:'post',
        data:{
            shopId:shopId,
        },
        success:function(data){
            if(data.code==1){
                rate=data.datas.bilv;
            }
        }
    })
}


var flag = true;
var count = 1;

//单价变化影响小计和提点变化
var goodsprice = document.getElementById('goodsprice');
goodsprice.oninput=function(){

    subTotal();

}
//输入数量影响变化
var goodsnum = document.getElementById('goodsnum');
goodsnum.onchange=function(){
    //判断输入数量是否为空或小于1
    if($('#goodsnum').val()==""||$('#goodsnum').val()==NaN||$('#goodsnum').val()<1){
        $('#goodsnum').val('1');
    }
    if($('#goodsnum').val()>1000){
        mui.toast('数量不能大于1000');
        $('#goodsnum').val('1000');
    }
    count = parseInt($('#goodsnum').val());
    if(count>1){
        $('.num-decrease').css('color','#49c9bb');
    }
    subTotal();

}

//减少商品数量
$('.num-decrease').click(function(){
    count--;
    if(count<1){
        count=1;
        $('.num-decrease').css('color','#bfbcc0');
    }
    if(count>1){
        $('.num-decrease').css('color','#49c9bb');
    }
    $('#goodsnum').val(count);
    subTotal();

})

//增加商品数量
$('.num-add').click(function(){
    count++;
    if(count>1000){
        count=1000;
        $('.num-decrease').css('color','#bfbcc0');
    }
    if(count>1){
        $('.num-decrease').css('color','#49c9bb');
    }
    $('#goodsnum').val(count);
    subTotal();

})

//商品小计
function subTotal(){
    //单价是否为空
    if($('#goodsprice').val()==""||$('#goodsprice').val()==undefined||$('#goodsprice').val()==null){
        $('.subtotal').html('0.00');
        $('.should-pay').html('0.00');
    }else{
        if($('#goodsprice').val()>1000000){
            mui.toast('单价不能大于一百万');
            $('.subtotal').html('0.00');
            $('.should-pay').html('0.00');
            return;
        }
        var price = (parseFloat( $('#goodsprice').val())*count).toFixed(2);
        $('.subtotal').html(price);
        $('.should-pay').html((price*rate).toFixed(2));
    }
}

//输入手机号验证客户是否存在
var vevityPhone = document.getElementById('phone');
vevityPhone.oninput=function(){
    if($('#phone').val().length=='11'){
        if(!ifCyber()){
            mui.toast('请检查网络并刷新重试');
        }
        $.ajax({
            url:'/user/checkUserisEmpty.json',
            dataType:'json',
            data:{
                userName:$('#phone').val(),
            },
            success:function(data){
                if(data.code==1){
                    $('#verityName').html(data.datas.realName);
                    $('#verityName').show();
                }
                if(data.code==0&&data.msg=='手机号不存在'){
                    $('#verityName').html('该用户未注册');
                    $('#verityName').show();
                    $('.code-p').show(200);
                }else{
                    $('.code-p').hide(200);
                }
            }
        })
    }
}

//防止password记住密码
$('#password').focus(function(){
    $('#password').attr('type','password');
})
//提交
$('.submit').click(function(){
    if($('#phone').val().length<'11'){
        mui.toast('手机号格式不正确');
        flag = true;
        return;
    }
    if($('#phone').val()==''){
        mui.toast('手机号不能为空');
        flag = true;
        return;
    }
    if($('#password').val()==''){
        mui.toast('交易密码不能为空');
        flag = true;
        return;
    }
    if($('#goodsprice').val()>1000000){
        mui.toast('单价不能大于一百万');
        $('.subtotal').html('0.00');
        $('.should-pay').html('0.00');
        return;
    }
    if($('#goodsnum').val()>1000){
        mui.toast('数量不能大于1000');
        $('.num-decrease').css('color','#bfbcc0');
    }
    if($('.should-pay').html()>1000000){
        mui.toast('实付金额不能大于一百万');
        flag = true;
        return;
    }
    if(flag!=true){
        mui.toast('正在请求，请稍候...');
        return;
    }
    flag=false;
    mui.confirm('您好，为保证消费者的合法权益，我们严厉打击虚假做单行为，请确定此单信息真实', '', ['取消','确定'], function(e) {
        if (e.index == 1) {
            if(!ifCyber()){
                mui.toast('请检查网络重试');
            }
            $.ajax({
                url:'/user/checkUserisEmpty.json',
                dataType:'json',
                data:{
                    userName:$('#phone').val(),
                },
                success:function(data){
                    if(data.code==1){
                        $.ajax({
                            url:'/user/userLoginKey.json',
                            dataType:'json',
                            data:{
                                name:getItem('phone'),
                            },
                            success:function(data) {
                                if (data.code != 1) {
                                    mui.toast(data.msg);
                                    flag = true;
                                    return;
                                }
                                if(data.code==1){
                                    var password = $('#password').val();
                                    password = calcMD5(password + data.datas.userKey, 32);
                                    $.ajax({
                                        url:'/zuoDanC/zuoDan.json',
                                        dataType:'json',
                                        data:{
                                            token:getItem('token'),
                                            name:$('#phone').val(),
                                            tradePassword:password,
                                            goodName:$('#goodsname').val(),
                                            goodOnePrice:$('#goodsprice').val(),
                                            goodNum:$('#goodsnum').val(),
                                            memo:$('#tips').val(),
                                        },
                                        type:'post',
                                        beforeSend:function(){
                                            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                                        },
                                        success:function(data){
                                            $('.loading2').hide();
                                            if(data.code==-1){
                                                window.location.href="login.html";
                                            }
                                            if(data.code==0){
                                                mui.toast(data.msg);
                                                flag = true;
                                                return;
                                            }
                                            if(data.code==1){
                                                mui.alert('做单成功',function(){
                                                    window.location.href="makeorder-list.html";
                                                });
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                    if(data.code==0&&data.msg=='手机号不存在'){
                        $.ajax({
                            url:'/user/checkMobileCode.json',
                            data:{
                                mobile:$('#phone').val(),
                                mobile_code:$('.code-input').val(),
                                platform:'wap',
                            },
                            dataType:'json',
                            success:function(data) {
                                if(data.code!=1){
                                    flag = true;
                                    mui.toast(data.msg);

                                }else{
                                    $.ajax({
                                        url:'/user/registerUser.json',
                                        data:{
                                            userName:$('#phone').val(),
                                        },
                                        dataType:'json',
                                        success:function(data){
                                            if(data.code!=1){
                                                flag = true;
                                                mui.toast(data.msg);

                                            }else{
                                                $.ajax({
                                                    url:'/user/userLoginKey.json',
                                                    dataType:'json',
                                                    data:{
                                                        name:getItem('phone'),
                                                    },
                                                    success:function(data) {
                                                        if (data.code != 1) {
                                                            mui.toast(data.msg);
                                                            flag = true;
                                                            return;
                                                        }
                                                        if(data.code==1){
                                                            var password = $('#password').val();
                                                            password = calcMD5(password + data.datas.userKey, 32);
                                                            $.ajax({
                                                                url:'/zuoDanC/zuoDan.json',
                                                                dataType:'json',
                                                                data:{
                                                                    token:getItem('token'),
                                                                    name:$('#phone').val(),
                                                                    tradePassword:password,
                                                                    goodName:$('#goodsname').val(),
                                                                    goodOnePrice:$('#goodsprice').val(),
                                                                    goodNum:$('#goodsnum').val(),
                                                                    memo:$('#tips').val(),
                                                                },
                                                                type:'post',
                                                                beforeSend:function(){
                                                                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                                                                },
                                                                success:function(data){
                                                                    $('.loading2').hide();
                                                                    if(data.code==-1){
                                                                        window.location.href="login.html";
                                                                    }
                                                                    if(data.code==0){
                                                                        mui.toast(data.msg);
                                                                        flag = true;
                                                                        return;
                                                                    }
                                                                    if(data.code==1){
                                                                        mui.alert('做单成功',function(){
                                                                            window.location.href="makeorder-list.html";
                                                                        });
                                                                    }
                                                                }
                                                            })
                                                        }
                                                    }
                                                })
                                            }

                                        }
                                    })
                                }

                            }
                        });
                    }
                }
            })
        } else {
            flag = true;
        }
    })

})


document.getElementById('code_input').oninput=function () {
    if($(this).val().length==5){
        $.ajax({
            //验证码是否正确接口
            url:'/imageVerifyC/verifyCode.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
                validateCode:$('#code_input').val(),
            },
            type:'post',
            success:function (data) {
                if(data.code!=1){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==1){
                    if(data.verifyCode){
                        $('.zhezhao').hide();
                        twice=true;
                        verifyCode = $('#code_input').val();
                        setItem('verifyCode',$('#code_input').val());
                        verify();
                    }else{
                        mui.toast('验证码错误');
                        var timestamp = (new Date()).valueOf();
                        $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                        return;
                    }
                }
            }
        })
    }
}

//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){
    if(!$('#phone').val().match(/^1[2-9][0-9]{9}$/)){
        mui.toast('请输入正确手机号码');
        return;
    }

    if(!ifCyber()){
        mui.toast('请检查网络');
    }

    //验证该手机号是否发送过验证码
    $.ajax({
        url:'/imageVerifyC/ifCode.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
        },
        type:'post',
        success:function (data) {
            if(data.ifCode){
                //如果为true，从本地获取图片验证码
                verifyCode = getItem('verifyCode');
                verify();
            }else{
                //如果为false，需要输入
                var timestamp = (new Date()).valueOf();
                $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                $('#code_input').val('');
                $('.zhezhao').show();
            }
        }
    })

}

function verify() {
    $(".code-button").html("发送中…");
    $(".code-button").unbind();
    $.ajax({
        url:'/user/sendMobileCode_ValidateCode.json',
        data:{
            mobile:$('#phone').val(),
            platform:'wap',
            validateCode:verifyCode,
        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                if(data.msg=='图形验证码验证失败'){
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val());
                    $('#code_input').val('');
                    $('.zhezhao').show();
                    return;
                }
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $(".code-button").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};



